package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/shoppingBodyAward/model"
	"gowork/shoppingBodyAward/utils"
	"time"
	"strconv"
)

type LinkageController struct {
	Ctx  iris.Context
	lkm *model.LinkageMessage
	cr *model.CallRecord

}

func (l *LinkageController) PostCreatlinkage() mvc.Result {

	w := new(utils.Worker)

	l.lkm = new(model.LinkageMessage)

	sendAccountIdStr := l.Ctx.FormValue("sendAccountId")
	sendAccountId, err := strconv.ParseInt(sendAccountIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	customerIdStr := l.Ctx.FormValue("customerId")
	customerId, err := strconv.ParseInt(customerIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	var lkmv = model.LinkageMessage{
		LinkageMessageId: w.GetId(),
		MessageTypeId : 3,
		SendAccountId : sendAccountId,
		SendAccountName : l.Ctx.FormValue("sendAccountName"),
		CustomerId : customerId,
		CustomerName : l.Ctx.FormValue("customerName"),
		SendMessageBody : l.Ctx.FormValue("sendMessageBody"),
		CreatedAt : time.Now(),
		UpdatedAt : time.Now(),
	}
	
	return mvc.Response{
        Object : l.lkm.CreatLinkage(&lkmv),
    }
}

func (l *LinkageController) PostInsertmessage() mvc.Result {

	linkageMessageIdStr := l.Ctx.FormValue("linkageMessageId")
	linkageMessageId, err := strconv.ParseInt(linkageMessageIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

    text := l.Ctx.FormValue("text")
	
	return mvc.Response{
        Object : l.lkm.ReplyMessage(linkageMessageId, text),
    }
}

func (l *LinkageController) PostInsertcall() mvc.Result {

	w := new(utils.Worker)

	customerIdStr := l.Ctx.FormValue("customerId")
	customerId, err := strconv.ParseInt(customerIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	accountIdStr := l.Ctx.FormValue("accountId")
	accountId, err := strconv.ParseInt(accountIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	cs, _ := strconv.Atoi(l.Ctx.FormValue("CallState"))
	ct ,_ := strconv.Atoi(l.Ctx.FormValue("CallTime"))

	var crv = model.CallRecord{
		CallRecordId: w.GetId(),
		CustomerId : customerId,
		CustomerName : l.Ctx.FormValue("CustomerName"),
		AccountId : accountId,
		CallState : cs,
		CallTime : ct,
		CreatedAt : time.Now(),
		UpdatedAt : time.Now(),
	}

	return mvc.Response{
        Object : l.cr.InsertCallRecord(&crv),
    }
}